#include <mysql/mysql.h>
#include <iostream>
#include <string>
#include <unistd.h>
int main()
{
    MYSQL *mfp = mysql_init(nullptr);
    if (mfp == nullptr)
    {
        std::cerr << "MYSQL INIT FALSE!" << std::endl;
        return -1;
    }
    MYSQL *conn = mysql_real_connect(mfp, "8.130.115.39", "connect", "Zyq20040814.", "test_connect", 3306, nullptr, 0);
    if (conn == nullptr)
    {
        std::cerr << "MYSQL CONNECT FALSE!" << std::endl;
        return -1;
    }
    mysql_set_character_set(mfp, "utf8");
    std::string op = "select * from people";
    if (mysql_query(mfp, op.c_str()) == 0)
    {
        std::cout << op.c_str() << " success!" << std::endl;
    }
    else
    {
        std::cerr << op.c_str() << " false!" << std::endl;
    }
    MYSQL_RES *res = mysql_store_result(mfp);
    int row = mysql_num_rows(res);
    int col = mysql_num_fields(res);
    //获取列名
    MYSQL_FIELD *field = mysql_fetch_fields(res);
    for (int i = 0; i < col; i++)
    {
        std::cout<<field->name<<"\t";
    }
    std::cout<<std::endl;
    for(int i=0;i<row;i++)
    {
        MYSQL_ROW line=mysql_fetch_row(res);;
        for(int j=0;j<col;j++)
        {
            std::cout<<line[j]<<"\t";
        }
        std::cout<<std::endl;
    }
    // std::string op="insert into people values(3)";
    // std::string op="delete from people where id=3";
    // while (true)
    // {
    //     std::cout<<"mysql->";
    //     std::string op;
    //     std::getline(std::cin, op);
    //     if (op == "quit")
    //     {
    //         std::cout << "bye bye" << std::endl;
    //         break;
    //     }
    //     if (mysql_query(mfp, op.c_str()) == 0)
    //     {
    //         std::cout << op.c_str() << " success!" << std::endl;
    //     }
    //     else
    //     {
    //         std::cerr << op.c_str() << " false!" << std::endl;
    //         break;
    //     }
    // }
    mysql_free_result(res);
    mysql_close(mfp);
    return 0;
}